import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = mpimg.imread('example.png')

lum_img = (img[:,:,0]+img[:,:,1]+img[:,:,2])/3 #(768, 1024)

core1=np.array([-1,-1,1,1]).reshape(2,2)
core2=np.array([1,-1,1,-1]).reshape(2,2)
core3=np.array([1,-1,-1,1]).reshape(2,2)

core=[core1,core2,core3]
nplum_img=np.array(lum_img)

h,w=nplum_img.shape

npresult=[np.zeros((h-1,w-1)),np.zeros((h-1,w-1)),np.zeros((h-1,w-1))]
for i in range(h-1):
    for k in range(w-1):
        v_lu=nplum_img[i,k]
        v_ru=nplum_img[i,k+1]
        v_ld=nplum_img[i+1,k]
        v_rd=nplum_img[i+1,k+1]
        window=np.array([v_lu,v_ru,v_ld,v_rd]).reshape(2,2)
        for j in range(3):
            npresult[j][i,k]=abs((window*core[j]).sum())

fig = plt.figure()
fig.add_subplot(2, 3, 2)
imgplot=plt.imshow(lum_img,cmap="gray")
fig.add_subplot(2, 3, 4)
imgplot=plt.imshow(npresult[0],cmap="gray")
fig.add_subplot(2, 3, 5)
imgplot=plt.imshow(npresult[1],cmap="gray")
fig.add_subplot(2, 3, 6)
imgplot=plt.imshow(npresult[2],cmap="gray")
fig.tight_layout()
plt.show()
